﻿namespace LeetCodeProject._0001_1000._001_100._51_60._060;

public class Solution
{
    public string GetPermutation(int n, int k)
    {
        var arr = new List<int>(n);
        for (int i = 1; i <= n; i++)
        {
            arr.Add(i);
        }

        var m = 1;
        for (int i = 1; i < n; i++)
            m *= i;
        var result = new char[n];
        k--;
        for (int i = 0, mm = n - 1; i < n; i++, mm--)
        {
            var idx = k / m;
            result[i] = (char)(arr[idx] + '0');
            arr.RemoveAt(idx);
            k %= m;
            if (mm > 0)
                m /= mm;
        }

        return new string(result);
    }
}